<?php
namespace app\api\controller\new;

use think\Controller;
use think\Request;
use app\common\model\contract\ProductContract;
use think\Db;
use app\common\model\power\Rolepower;

class Contract extends Controller
{
    public function initialize()
    {
        parent::initialize();
        $this->is_login();
        Rolepower::powerguard('305001'); // 销售合同管理权限
    }
    private function is_login()
    {
        $data = \app\common\model\user\User::is_sessionlogin();
        if (!$data['code']) {
            $data['options'] = 'relogin';
            json($data)->send();
            exit;
        }
    }
    // 合同列表/添加/修改（305001）
    public function contractList(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 305) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $group_id = Db::name('user_group')->where('user_id', $loginUser['id'])->value('group_id');
        if (!$group_id) {
            return json(['code'=>0, 'msg'=>'未绑定销售组']);
        }
        $user_ids = Db::name('user_group')->where('group_id', $group_id)->column('user_id');
        $customer_ids = Db::name('user_customer')->whereIn('user_id', $user_ids)->column('customer_id');
        $list = ProductContract::whereIn('user_id', $user_ids)->select();
        return json(['code'=>1, 'msg'=>'success', 'data'=>$list]);
    }

    // 合同添加
    public function contractAdd(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 305) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $data = $request->post();
        if (empty($data['customer_id']) || empty($data['name']) || empty($data['remark']) || empty($data['img_url'])) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $contract = new ProductContract();
        $contract->user_id = $loginUser['id'];
        $contract->contract_no = ProductContract::generateContractNo();
        $contract->remark = $data['remark'];
        $contract->img_url = $data['img_url'];
        $contract->payment_time = $data['payment_time'] ?? null;
        $contract->payment_status = $data['payment_status'] ?? 0;
        $contract->ops_time = $data['ops_time'] ?? null;
        $contract->ops_price = $data['ops_price'] ?? 0.00;
        $contract->ops_endtime = $data['ops_endtime'] ?? null;
        $contract->ops_cycletime = $data['ops_cycletime'] ?? 0;
        $contract->status = 1;
        $contract->create_time = time();
        $contract->update_time = time();
        $contract->save();
        return json(['code'=>1, 'msg'=>'添加成功']);
    }

    // 合同修改
    public function contractEdit(Request $request)
    {
        $loginUser = session('user');
        if (!$loginUser || $loginUser['role'] != 305) {
            return json(['code'=>0, 'msg'=>'无权限']);
        }
        $data = $request->post();
        if (empty($data['id'])) {
            return json(['code'=>0, 'msg'=>'参数不完整']);
        }
        $contract = ProductContract::get($data['id']);
        if (!$contract) {
            return json(['code'=>0, 'msg'=>'合同不存在']);
        }
        $contract->remark = $data['remark'] ?? $contract->remark;
        $contract->img_url = $data['img_url'] ?? $contract->img_url;
        $contract->payment_time = $data['payment_time'] ?? $contract->payment_time;
        $contract->payment_status = $data['payment_status'] ?? $contract->payment_status;
        $contract->ops_time = $data['ops_time'] ?? $contract->ops_time;
        $contract->ops_price = $data['ops_price'] ?? $contract->ops_price;
        $contract->ops_endtime = $data['ops_endtime'] ?? $contract->ops_endtime;
        $contract->ops_cycletime = $data['ops_cycletime'] ?? $contract->ops_cycletime;
        $contract->update_time = time();
        $contract->save();
        return json(['code'=>1, 'msg'=>'修改成功']);
    }
} 